#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QString>
#include <QPushButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    // ui->setupUi(this);
    // QPushButton *button = new QPushButton("close button", this);
    // resize(800, 600);
    // button->setCheckable(true);
    //这样不行，oldState和newState的状态是在connect(...)执行时才获取的，换句话说，isChecked()函数
    //是在connect(...)执行时才产生结果，所以二者的值是一样的
    // button->setChecked(false);
    // bool oldState = button->isChecked();
    // connect(button, &QPushButton::toggled, this, &QWidget::close);
    // bool newState = button->isChecked();
    // if (oldState != newState)
    // {
    //     qDebug() << "state changed";
    // }
    // connect(button, &QPushButton::toggled, this, [=](bool checked){
    //     qDebug() << "state changed to" << checked;
    //     if (checked) {
    //         this->close();  // 只有切换到选中时关闭窗口，也可以按需改
    //     }
    // });
    // connect(this,&Widget::MySignal,this,&Widget::MySlots);
    // EmitSignal();

    // this->tea = new Teacher(this);
    // this->stu = new Student(this);
    // QPushButton *button = new QPushButton("上课了", this);
    // button->move(200, 300);

    // connect(tea, &Teacher::MySignal, stu, &Student::StartStudy);
    // // connect(button, &QPushButton::clicked, tea, &Teacher::MySignal);
    // connect(button, &QPushButton::clicked, stu, &Student::StartStudy);
    // EmitSignal();

    // void (Widget::*Signal_p)(QString) = &Widget::MySignal;
    // void (Widget::*Slot_p)(QString) = &Widget::MySlots;
    // qDebug() << "hello2";
    // connect(this,Signal_p,this,Slot_p);
    // qDebug() << "hello1";
    // QPushButton *button = new QPushButton("This is a button",this);
    // this->resize(800,600);
    // button->move(200,300);
    // connect(this, QOverload<QString>::of(&Widget::MySignal), this, QOverload<QString>::of(&Widget::MySlots));

    // connect(button, &QPushButton::clicked, this, QOverload<>::of(&Widget::MySignal));
    // EmitSignal();

    // 信号连接信号一般用于解耦
    // QPushButton *button = new QPushButton("This is a button",this);
    // this->resize(800,600);
    // button->move(200,300);
    // connect(this, QOverload<>::of(&Widget::MySignal), this, &Widget::ForwardedSignal);
    // connect(this, &Widget::ForwardedSignal, this, QOverload<>::of(&Widget::MySlots));
    // connect(button, &QPushButton::clicked, this, &Widget::EmitSignal);
    //一个信号连接多个槽
    QPushButton *button = new QPushButton("This is a button",this);
    this->resize(800,600);
    button->move(200,300);
    // connect(button, &QPushButton::clicked, this, QOverload<>::of(&Widget::MySlot1));
    // connect(button, &QPushButton::clicked, this, QOverload<>::of(&Widget::MySlot2));
    // connect(button, &QPushButton::clicked, this, QOverload<>::of(&Widget::MySlot3));
    // EmitSignal();
    connect(this, QOverload<>::of(&Widget::MySignal1), this, QOverload<>::of(&Widget::MySlot3));
    connect(this, QOverload<>::of(&Widget::MySignal2), this, QOverload<>::of(&Widget::MySlot3));
    connect(this, SIGNAL(&Widget::MySignal1), this, SLOT(&Widget::MySlot3()));
    EmitSignal();
}
void Widget::EmitSignal()
{
    // emit tea->MySignal();
    qDebug() << "signal to signal";
    emit this->MySignal1();
    emit this->MySignal2();
}

void Widget::MySlots()
{
    qDebug() << "my signal and slot";
}
void Widget::MySlots(QString)
{
    qDebug() << "Widget::MySlots(QString)";
}

void Widget::MySlot1()
{
    qDebug() << "Widget::MySlot1()";
}
void Widget::MySlot2()
{
    qDebug() << "Widget::MySlot2()";
}
void Widget::MySlot3()
{
    qDebug() << "Widget::MySlot3()";
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    this->close();
}

